package scala.function

import scala.collection.mutable.ArrayBuffer

/**
 * 高阶函数案例(集合再讲)
 */
case object FunctionDeclareTest09 {
  def main(args: Array[String]): Unit = {
    //    (1)map映射
    def map(arr: Array[Int], op: Int => Int) = {
      for (elem <- arr) yield op(elem)
    }

    val arr = map(Array(1, 2, 3, 4), (x: Int) => {
      x * x
    })
    println(arr.mkString(","))

    //   (2)filter过滤，有参数，且参数在后面只使用一次，则参数省略且后面的参数用_表示
    def filter(arr: Array[Int], op: Int => Boolean) = {
      val arrayBu: ArrayBuffer[Int] = ArrayBuffer[Int]()
      for (em <- arr if op(em)) arrayBu.append(em)
      arrayBu.toArray
    }

    var arr1 = filter(Array(1, 2, 3, 4), _ % 2 == 1)
    var arr2 = filter(Array(4, 22, 31, 43), _ >= 20)
    println(arr1.mkString(","))
    println(arr2.mkString(","))

    //    (3)reduce聚合，有多个参数，且每个参数在后面只使用一次，则参数省略且后面参数用_表示，第n个_代表第n个参数
    def reduce(arr: Array[Int], op: (Int, Int) => Int) = {
      var init: Int = arr(0)
      for (elem <- 1 until arr.length) {
        init = op(init, elem)
      }
      init
    }

    val arr23 = reduce(Array(1, 2, 3, 4), _ * _)
    println(arr23)


  }
}
